//
//  KDRelationCell.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/11/17.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDRelationCell.h"
#import "KDMListCellBaseModel.h"
#import "SVProgressHUD.h"
#import "KDHomeRequestTool.h"
#import "KDHomeListPageCellModel.h"
#import "KDHomeListPageCollectParam.h"
#import "KDHomeListPageCollectResult.h"
#import "KDHomeListPageUnCollectResult.h"
#import "KDHomeListPageUnCollectParam.h"
@interface KDRelationCell ()
@property (weak, nonatomic) IBOutlet UIButton *titleBtn;
@property (weak, nonatomic) IBOutlet UIButton *timeLabel;
@property (weak, nonatomic) IBOutlet UIButton *collectBtn;
@property (weak, nonatomic) IBOutlet UIButton *reportBtn;
/**
 *  来源label
 */
@property (weak, nonatomic) IBOutlet UILabel *comeFromLabel;
/**
 *  作者label
 */
@property (weak, nonatomic) IBOutlet UILabel *authorLabel;

@property (weak, nonatomic) IBOutlet UIView *underView;

@end
@implementation KDRelationCell

- (void)awakeFromNib {
    // Initialization code
}
- (void)setHighlight:(BOOL)highlight{
    _highlight = highlight;
    [self.titleBtn setTitleColor:WQColor(178, 182, 184) forState:UIControlStateNormal];
    //    [self layoutIfNeeded];
}
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *cellId =@"KDRelationCell";
    UINib *xib = [UINib nibWithNibName:@"KDRelationCell" bundle:nil];
    [tableView registerNib:xib forCellReuseIdentifier:cellId];
    KDRelationCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell =[[[NSBundle mainBundle]loadNibNamed:@"KDRelationCell" owner:self options:nil]lastObject];
    }
    //    HomeCell2 *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    cell.contentView.backgroundColor = BackGroundColor;
    //    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
    cell.titleBtn.titleLabel.numberOfLines = 2;
    cell.underView.layer.cornerRadius = 5.0f;
    cell.underView.layer.borderWidth = 0.2f;
    cell.underView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    //监听收藏状态改变之后的通知
    [[NSNotificationCenter defaultCenter] addObserver:cell selector:@selector(SingleWebViewControllerCollectionBtnClick:) name:@"SingleWebViewControllerCollectionBtnClickToHomeNoty" object:nil];
    
    //监听上报成功之后的通知
    [[NSNotificationCenter defaultCenter] addObserver:cell selector:@selector(ReportControllerReportSeccess:) name:@"ReportControllerReportSeccessToRelationNoty" object:nil];
    return cell;
}
- (void)setModel:(KDMListCellBaseModel *)model{
    _model = model;
    [self setNeedsLayout];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    if (self.model) {
        NSString * str = [self.model.title stringByReplacingOccurrencesOfString:@"<em>" withString:@""];
        NSString *title = [str stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
        [self.titleBtn setTitle:title forState:UIControlStateNormal];
        if (self.model.isRead) {
            [self.titleBtn setTitleColor:WQColor(178, 182, 184) forState:UIControlStateNormal];
        }
        [self.timeLabel setTitle:self.model.date forState:UIControlStateNormal];
        self.collectBtn.selected = self.model.isSave;
        self.reportBtn.selected = self.model.isReport;
        self.comeFromLabel.text = self.model.source;
        self.authorLabel.text = self.model.author;
        //        if (CGRectGetMaxX(self.timeLabel.frame) > self.collectBtn.x) {
        //            NSString *str = [self.model.date substringToIndex:10];
        //            self.model.date = str;
        //             [self.timeLabel setTitle:self.model.date forState:UIControlStateNormal];
        //        }
    }
    
}
#pragma mark - 点击事件区
/*
**
*  点击收藏按钮
*/
- (IBAction)collectBtnClick:(id)sender {
    
    if (!self.collectBtn.selected) {//点中收藏
        //给服务器发送“已收藏的信息”
        [self sendCollectData];
    }else{//取消收藏
        [self sendUncollectData];
    }
    
}
- (void)sendCollectData
{
    KDAccount *account = [KDAccountTool account];
    KDHomeListPageCollectParam *param = [[KDHomeListPageCollectParam alloc]init];
    param.method = @"addFavorite";
    param.docId = self.model.docId;
    param.url = self.model.url;
    param.token = account.token;
    [KDHomeRequestTool sendCollectDataWithParam:param success:^(KDHomeListPageCollectResult *responseObejct) {
        [SVProgressHUD showSuccessWithStatus:@"收藏成功！" maskType:SVProgressHUDMaskTypeGradient];
        self.model.save = !self.model.save;
        self.collectBtn.selected = self.model.isSave;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"RelationControllerCollectionBtnClickToMonitorNoty" object:nil userInfo:@{@"docid":self.model.docId, @"isCollect" :@(self.model.save)}];

    } faiure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"收藏失败！" maskType:SVProgressHUDMaskTypeGradient];
        KDLog(@"%@",error);
    }];
}
- (void)sendUncollectData{
    KDAccount *account = [KDAccountTool account];
    KDHomeListPageUnCollectParam *param = [[KDHomeListPageUnCollectParam alloc]init];
    param.method = @"deleteFav";
    param.token = account.token;
    param.docId = self.model.docId;
    [KDHomeRequestTool sendUnCollectDataWithParam:param success:^(KDHomeListPageUnCollectResult *responseObejct) {
        [SVProgressHUD showSuccessWithStatus:@"取消收藏成功！" maskType:SVProgressHUDMaskTypeGradient];
        self.model.save = !self.model.save;
        self.collectBtn.selected = self.model.isSave;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"RelationControllerCollectionBtnClickToMonitorNoty" object:nil userInfo:@{@"docid":self.model.docId, @"isCollect" :@(self.model.save)}];
    } faiure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"取消收藏失败！" maskType:SVProgressHUDMaskTypeGradient];
        KDLog(@"%@",error);
    }];
    
}
- (IBAction)reportBtnClick:(id)sender {
    
    if (_delegate &&[_delegate respondsToSelector:@selector(KDRelationCell:reportBtnClick:)]) {
        [_delegate KDRelationCell:self reportBtnClick:sender];
    }
    
}

#pragma mark - 通知接收区
- (void)SingleWebViewControllerCollectionBtnClick:(NSNotification *)noty{
    NSString *docid = noty.userInfo[@"docid"];
    if ([docid isEqualToString: self.model.docId]) {
        self.model.save = [noty.userInfo[@"isCollect"] boolValue];
        self.collectBtn.selected = self.model.isSave;
    }
}
- (void)ReportControllerReportSeccess:(NSNotification *)noty{
    NSString *docid = noty.userInfo[@"docid"];
    if ([docid isEqualToString: self.model.docId]) {
        self.model.report = YES;
        self.reportBtn.selected = self.model.report;
    }
}
@end
